package com.liuxing.javaSpringBoot.modules.test.controller;

import com.liuxing.javaSpringBoot.modules.common.entity.Result;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import com.liuxing.javaSpringBoot.modules.test.entity.Student;
import com.liuxing.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by IntelliJ IDEA.
 * @File StudentController.java
 * @Auth liuxing
 * @Date 2021/8/20 13:33
 * @Email liuxing997@foxmail.com
 */
@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

     /**
     * 添加、编辑学生信息
     * 127.0.0.1/api/student  --- post
     * {"name":"liuxing", "age":18, "email":"liuxing997@foxmail.com"}
     * {"name":"liuxing1", "age":23, "email":"liuxing997@foxmail.com","studentCard":{"cardNumber":"201806134114"}}
     */
    @PostMapping(value = "/student",consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody  Student student) {
        return studentService.editStudent(student);
    }

     /**
     * 编辑学生信息
     * 127.0.0.1/api/student  --- put
     * {"id":1,"name":"liuxing", "age":18, "email":"liuxing997@foxmail.com"}
     */
    @PutMapping(value = "/student",consumes = "application/json")
    public Result<Student> editStudent(@RequestBody  Student student) {
        return studentService.editStudent(student);
    }


     /**
     * 根据ID删除学生信息
     * 127.0.0.1/api/13  --- delete
     */
    @DeleteMapping(value = "/student/{id}")
    public  Result<Object> deleteStudentById(@PathVariable int id){
        return studentService.deleteStudentById(id);
    }

    /**
     * 根据ID查询学生信息
     * 127.0.0.1/api/1  --- get
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    /**
     * 查询所有学生信息
     * 127.0.0.1/api/students  --- get
     */
    @GetMapping("/students")
    public List<Student> getStudents(){
      return studentService.getStudents();
    }

    /**
     * 根据姓名和邮箱学生信息
     * 127.0.0.1/api/student  --- get
     */
    @GetMapping("/student")
    public Student getStudentByNameAndEmail(@RequestParam String name,
                                            @RequestParam String email) {
        return studentService.getStudentByNameAndEmail(name,email);
    }

    /**
     * 多条件分页查询学生信息
     * 127.0.0.1/api/students  --- post
     * {"currentPage":2,"pageSize":10,"orderBy":"name","direction":"desc","keyword":"l"}
     */
    @PostMapping(value = "/students",consumes = "application/json")
    public Page<Student> getStudentBySearchBen(@RequestBody  SearchBean searchBean) {
        return studentService.getStudentBySearchBean(searchBean);
    }

    /**
     * 批量插入学生信息
     * 127.0.0.1/api/students/v2  --- post
     * [{"name":"liuxing13", "age":23, "email":"liuxing997@foxmail.com","studentCard":{"cardNumber":"201806134114"}},
     * {"name":"liuxing14", "age":23, "email":"liuxing997@foxmail.com","studentCard":{"cardNumber":"201806134114"}},
     * {"name":"liuxing15", "age":23, "email":"liuxing997@foxmail.com","studentCard":{"cardNumber":"201806134114"}}]
     */
    @PostMapping(value = "/students/v2",consumes = "application/json")
    public Result<Object> insertStudents(@RequestBody List<Student> students){
        return studentService.insertStudents(students);
    }

}
